package com.changgou.file.controller;


import com.changgou.entity.Result;

import com.changgou.entity.StatusCode;
import com.changgou.file.util.FastDFSClient;
import com.changgou.file.util.FastDFSFile;
import jdk.net.SocketFlow;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



@RestController
@RequestMapping("/file")
public class FileController {
    @PostMapping("/upLoad")
    public Result upLoad(MultipartFile file) throws Exception {
        if (file.getSize()==0){
            throw new RuntimeException("文件不存在");
        }
        if (file==null){
            throw new RuntimeException("文件不存在");
        }
        String originalFilename = file.getOriginalFilename();
        String endName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        byte[] bytes = file.getBytes();
        FastDFSFile fastDFSFile = new FastDFSFile(originalFilename,bytes,endName);
        //基于工具类进行文件上传,并接受返回参数  String[]
        String[] upLoadResult = FastDFSClient.upload(fastDFSFile);
        String url = FastDFSClient.getTrackerUrl()+upLoadResult[0] + "/" + upLoadResult[1];
        return  new Result(true, StatusCode.OK,"isok",url);
    }
}
